package com.mall.seckill.service.impl;

import com.mall.seckill.model.SeckillOrder;
import com.mall.seckill.mapper.SeckillOrderMapper;
import com.mall.seckill.service.ISeckillOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 秒杀订单表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-09-28
 */
@Service
public class SeckillOrderServiceImpl extends ServiceImpl<SeckillOrderMapper, SeckillOrder> implements ISeckillOrderService {
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addSeckillOrder(SeckillOrder seckillorder){
        return super.save(seckillorder);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(String id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateSeckillOrder(SeckillOrder seckillorder){
        return super.updateById(seckillorder);
    }

    @Override
    public SeckillOrder getById(String id){
        return super.getById(id);
    }

    @Override
    public List<SeckillOrder> selectList(SeckillOrder seckillorder){
        return super.list(getQueryWrapper(seckillorder));
    }

    @Override
    public Page<SeckillOrder> selectPage(Page<SeckillOrder> page, SeckillOrder seckillorder){
        return super.page(page, getQueryWrapper(seckillorder));
    }

    public LambdaQueryWrapper<SeckillOrder> getQueryWrapper(SeckillOrder seckillorder){
        return Wrappers.<SeckillOrder>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(seckillorder.getId()), SeckillOrder::getId, seckillorder.getId())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getSeckillGoodsId()), SeckillOrder::getSeckillGoodsId, seckillorder.getSeckillGoodsId())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getMoney()), SeckillOrder::getMoney, seckillorder.getMoney())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getUsername()), SeckillOrder::getUsername, seckillorder.getUsername())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getPayTime()), SeckillOrder::getPayTime, seckillorder.getPayTime())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getStatus()), SeckillOrder::getStatus, seckillorder.getStatus())
            .eq(ObjectUtils.isNotEmpty(seckillorder.getWeixinTransactionId()), SeckillOrder::getWeixinTransactionId, seckillorder.getWeixinTransactionId())
        ;
    }
}
